package com.example.demo.controller.admin;

import com.example.demo.entity.Privilege;
import com.example.demo.pojo.BSResult;
import com.example.demo.pojo.ZTreeNode;
import com.example.demo.service.PrivilegeService;
import com.example.demo.utils.BSResultUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/admin/privilege")
@RequiresPermissions("privilege-manage")
@Controller
public class AdminPrivilegeController {

    @Autowired
    private PrivilegeService privilegeService;

    @RequiresPermissions("privilege-list")
    @RequestMapping("/list")
    public String privilegeList(){

        return "/admin/privilege/list";
    }

    @RequestMapping("/treeNodes")
    @ResponseBody
    public List<ZTreeNode> treeNodeListJSON(){
        return privilegeService.getZTreeNodes();
    }


    @RequestMapping("/edit")
    @ResponseBody
    public BSResult edit(Privilege privilege){

        return privilegeService.updatePrivilege(privilege);
    }

    @RequestMapping("/{privilegeId}")
    @ResponseBody
    public BSResult getPrivilegeById(@PathVariable("privilegeId")Integer privilegeId){

        Privilege privilege=privilegeService.findById(privilegeId);

        return BSResultUtils.success(privilege);

    }

    @RequestMapping("/addition")
    @ResponseBody
    public BSResult addPrivilege(Privilege privilege){


    return privilegeService.addPrivilege(privilege);

    }

    @RequestMapping("/deletion/{privId}")
    @ResponseBody
    public BSResult deletePrivilege(@PathVariable("privId") int privId){
        BSResult bsResult = privilegeService.deleteById(privId);
        return bsResult;
    }


    @RequestMapping("/toEdit/{roleId}")
    @RequiresPermissions("privilege-edit")
    public String toEditPrivilege(@PathVariable("roleId") int roleId,Model model){
        model.addAttribute("roleId", roleId);
        return "admin/privilege/edit";
    }

    @RequestMapping("/rolePrivileges/{roleId}")
    @ResponseBody
    public Object rolePrivileges(@PathVariable("roleId") int roleId){

        return privilegeService.findPrivilegeByRoleId(roleId);



    }





}
